package com.kuaidao.sys.controller.dictiondry;


import com.kuaidao.common.constant.SysErrorCodeEnum;
import com.kuaidao.common.entity.IdEntity;
import com.kuaidao.common.entity.IdsEntity;
import com.kuaidao.common.entity.JSONResult;
import com.kuaidao.common.entity.PageBean;
import com.kuaidao.common.util.CommonUtil;
import com.kuaidao.sys.config.DictionaryErrorCode;
import com.kuaidao.sys.dto.dictionary.DictionaryAddUpdateDTO;
import com.kuaidao.sys.dto.dictionary.DictionaryReqDTO;
import com.kuaidao.sys.dto.dictionary.DictionaryResDTO;
import com.kuaidao.sys.entity.user.Dictionary;
import com.kuaidao.sys.service.dictiondry.IDictiondryService;
import com.kuaidao.sys.util.IdUtil;
import org.apache.commons.collections.CollectionUtils;
import org.ehcache.core.internal.util.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dictionary")
public class DictionaryController {


    @Autowired
    private IDictiondryService iDictiondryService;


    @PostMapping("/insertDictiondrt")
    public JSONResult insertDictiondrt(@Valid @RequestBody DictionaryAddUpdateDTO dictionaryAddUpdateDTO, BindingResult result){


        /**
         * @Valid 参数校验
         */
        if (result.hasErrors()){
            return CommonUtil.validateParam(result);
           }



        //code唯一校验
        Map<String, Object> paramMap =new HashMap<>();
        paramMap.put("code",dictionaryAddUpdateDTO.getCode());

        List dicList = iDictiondryService.findEntityListByParams(paramMap);
        if(CollectionUtils.isNotEmpty(dicList)){
            return new JSONResult().fail(DictionaryErrorCode.ERR_DIC_CODE_EXISTS.getCode(),
                    DictionaryErrorCode.ERR_DIC_CODE_EXISTS.getMessage());
        }

        Dictionary dictionary =new Dictionary();
        BeanUtils.copyProperties(dictionaryAddUpdateDTO,dictionary);
        dictionary.setId(IdUtil.getUUID());
        dictionary.setCreateTime(new Date());
        boolean b = iDictiondryService.insertDictiondrt(dictionary);
        return new JSONResult().success(b);
}


    @PostMapping("/findDictionaryByPage")
    public JSONResult<PageBean<DictionaryResDTO>> findDictionaryByPage(@RequestBody DictionaryReqDTO dictionaryReqDTO){
        List dictionaryByPage = iDictiondryService.findDictionaryByPage(dictionaryReqDTO);

        PageBean pageBen=new PageBean<>(dictionaryByPage);

        return  new JSONResult<PageBean<DictionaryResDTO>>().success(pageBen);
    }

    /**
     *  根据ID查询主题信息
     * @param
     * @return
     */
    @RequestMapping("/findDictionaryByPrimaryKey")
    public JSONResult<DictionaryResDTO> findDictionaryByPrimaryKey(@RequestBody IdEntity idEntity){

        Dictionary dictionaryByPrimaryKey = iDictiondryService.findDictionaryByPrimaryKey(idEntity.getId());


        DictionaryResDTO dictionaryResDTO =new DictionaryResDTO();
        BeanUtils.copyProperties(dictionaryByPrimaryKey,dictionaryResDTO);

        return new JSONResult().success(dictionaryResDTO);
    }

    /**
     *  根据ID更新字典主题信息
     * @param dictionaryAddUpdateDTO
     * @param result
     * @return
     */
    @PostMapping("/updateDictionary")
    public JSONResult updateDictionary(@Valid @RequestBody DictionaryAddUpdateDTO dictionaryAddUpdateDTO,BindingResult result){

        /**
         * 1.字段校验  2.code唯一  3.更新
         */

        if (result.hasErrors()){
            return CommonUtil.validateParam(result);
        }

        //code唯一校验
        Map<String, Object> paramMap =new HashMap<>();
        paramMap.put("code",dictionaryAddUpdateDTO.getCode());
        List<Dictionary> dicList = iDictiondryService.findEntityListByParams(paramMap);

        if(CollectionUtils.isNotEmpty(dicList)){
            for (Dictionary dictionary : dicList) {
                if (!dictionaryAddUpdateDTO.getId().equals(dictionary.getId())){
                    return new JSONResult().fail(DictionaryErrorCode.ERR_DIC_CODE_EXISTS.getCode(),
                            DictionaryErrorCode.ERR_DIC_CODE_EXISTS.getMessage());
                }
            }
        }

        Dictionary dictionary=new Dictionary();
        BeanUtils.copyProperties(dictionaryAddUpdateDTO,dictionary);
        dictionary.setUpdateTime(new Date());
        boolean b=iDictiondryService.updateDictionary(dictionary);

        return new JSONResult().success(b);
    }


    /**
     * 根据ID删除字典主题
     * @param primaryKey
     * @return
     */
    @PostMapping("/deleteDictionarys")
    public JSONResult deleteDictionarys(@RequestBody IdsEntity idsEntity){
        boolean b = iDictiondryService.deleteDictionarys(idsEntity);
        return new JSONResult<>().success(b);
    }


}


